package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.SelfExceptionHandler;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-03-05
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    //查询小节的service
    @Autowired
    private EduVideoService videoService;


    /**
     * 根据课程Id获取大纲列表
     * @param courseId
     * @return
     */
    @Override
    public List getChapterByCourseId(String courseId) {

        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        //先查询章节
        List<EduChapter> eduChapters = baseMapper.selectList(queryWrapper);
        //查询小节
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        List<EduVideo> eduVideos = videoService.list(wrapper);

        //封装最后的数据,嵌套for循环
        List<ChapterVo> list =new ArrayList<>();
        for(EduChapter chapter:eduChapters)
        {
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            list.add(chapterVo);
           //一个章节有多个小节，封装小节
            List<VideoVo> videoVoList=new ArrayList<>();
            for(int i=0;i<eduVideos.size();i++)
            {
                EduVideo eduVideo=eduVideos.get(i);
                //判断：小节里面的chapter_id和章节里面的id是否一样
                if(chapterVo.getId().equals(eduVideo.getChapterId()))
                {
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);



        }

        return list;
    }

    /**
     * 删除章节和章节下面的小节
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapterAndVideo(String chapterId) {
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count= videoService.count(wrapper);
        if(count!=0)
        videoService.remove(wrapper);

        int i= baseMapper.deleteById(chapterId);
        if(i==0)
        {
            throw new SelfExceptionHandler("删除失败",20001);
        }

        return true;
    }


}
